package com.yuehuanghun.admin.quartz.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yuehuanghun.admin.core.config.util.poi.ExcelUtil;
import com.yuehuanghun.admin.quartz.domain.SysJobLog;
import com.yuehuanghun.admin.quartz.service.ISysJobLogService;
import com.yuehuanghun.framework.annotation.Log;
import com.yuehuanghun.framework.annotation.Log.BusinessType;
import com.yuehuanghun.framework.domain.PageDTO;
import com.yuehuanghun.framework.web.BaseResource;

import cn.dev33.satoken.annotation.SaCheckPermission;

/**
 * 调度日志操作处理
 * 
 * @author yadmin
 */
@RestController
@RequestMapping("/monitor/jobLog")
public class SysJobLogController extends BaseResource {
	@Autowired
	private ISysJobLogService jobLogService;

	/**
	 * 查询定时任务调度日志列表
	 */
	@GetMapping("/list")
	@SaCheckPermission("monitor:job:list")
	public PageDTO<SysJobLog> list(SysJobLog sysJobLog) {
		List<SysJobLog> list = jobLogService.getByExample(sysJobLog, getPage());
		return toPage(list);
	}

	/**
	 * 导出定时任务调度日志列表
	 */
	@Log(title = "任务调度日志", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	@SaCheckPermission("monitor:job:export")
	public String export(SysJobLog sysJobLog) {
		List<SysJobLog> list = jobLogService.getByExample(sysJobLog);
		ExcelUtil<SysJobLog> util = new ExcelUtil<SysJobLog>(SysJobLog.class);
		return util.exportExcel(list, "调度日志");
	}

	/**
	 * 根据调度编号获取详细信息
	 */
	@GetMapping(value = "/{configId}")
	@SaCheckPermission("monitor:job:query")
	public SysJobLog getInfo(@PathVariable Long jobLogId) {
		return jobLogService.getById(jobLogId).orElse(null);
	}

	/**
	 * 删除定时任务调度日志
	 */
	@Log(title = "定时任务调度日志", businessType = BusinessType.DELETE)
	@DeleteMapping("/{jobLogIds}")
	@SaCheckPermission("monitor:job:remove")
	public void remove(@PathVariable List<Long> jobLogIds) {
		jobLogService.deleteByIds(jobLogIds);
	}

	/**
	 * 清空定时任务调度日志
	 */
	@Log(title = "调度日志", businessType = BusinessType.CLEAN)
	@DeleteMapping("/clean")
	@SaCheckPermission("monitor:job:remove")
	public void clean() {
		jobLogService.deleteByCriteria(p -> {});
	}
}
